package edu.scau.mis.web.controller.pos;

import edu.scau.mis.pos.entity.PosOrderItem;
import edu.scau.mis.pos.vo.PosResultVo;
import edu.scau.mis.pos.service.IOrderItemService;
import edu.scau.mis.pos.vo.DashboardVo;
import edu.scau.mis.pos.vo.OrderItemVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

/**
 * (PosOrderItem)表控制层
 *
 * @author makejava
 * @since 2021-11-10 17:16:18
 */
@RestController
@RequestMapping("orderItem")
public class OrderItemController {
    /**
     * 服务对象
     */
    @Resource
    private IOrderItemService orderItemService;

    /**
     * 分页查询
     *
     * @param posOrderItem 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public ResponseEntity<List<PosOrderItem>> list(PosOrderItem posOrderItem) {
        return ResponseEntity.ok(this.orderItemService.queryAll(posOrderItem));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param orderItemId 主键
     * @return 单条数据
     */
    @GetMapping("{orderItemId}")
    public PosOrderItem getById(@PathVariable("orderItemId") Integer orderItemId) {
        return this.orderItemService.queryById(orderItemId);
    }

    /**
     * 新增数据
     *
     * @param posOrderItem 实体
     * @return 新增结果
     */
    @PostMapping
    public String add(@RequestBody PosOrderItem posOrderItem) {
        this.orderItemService.insert(posOrderItem);
        return "ok";
    }

    /**
     * 编辑数据
     *
     * @param posOrderItem 实体
     * @return 编辑结果
     */
    @PutMapping
    public String edit(@RequestBody PosOrderItem posOrderItem) {
        this.orderItemService.update(posOrderItem);
        return "ok";
    }

    /**
     * 删除数据
     *
     * @param orderItemId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{orderItemId}")
    public Boolean deleteById(@PathVariable Integer orderItemId) {
        return this.orderItemService.deleteById(orderItemId);
    }




    /**
     * 服务对象
     */
    @Resource
    private IOrderItemService orderITemService;

    /**
     * 步骤1:创建订单信息
     *
     * @return 订单编号、创建时间、初始化总金额和总数量
     */
    @GetMapping("/createOrder")
    public ResponseEntity<Map<String,Object>>createOrder() {
        return ResponseEntity.ok(this.orderITemService.createOrder());
    }

    /**
     * 返回待购买商品总数及总金额
     *
     * @return 待购买商品总数及总金额
     */
    @GetMapping("/getOrderDetails")
    public ResponseEntity<Map<String,Object>>getOrderDetails() {
        return ResponseEntity.ok(this.orderITemService.getTotalQuantityAndTotalAmount());
    }

    /**
     * 步骤2:输入代购买商品编号、数量
     *
     * @param orderItemVo 传值对象
     * @return 新增结果
     */
    @GetMapping ("/enterItem")
    public ResponseEntity<PosResultVo> enterItem(OrderItemVo orderItemVo) {
        return ResponseEntity.ok(this.orderITemService.enterLineOrderItem(orderItemVo));
    }

    /**
     * 确认购买
     * 步骤3
     * @return 新增结果
     */
    @GetMapping("/confirmPurchase")
    public ResponseEntity<Boolean> confirmPurchase() {
        return ResponseEntity.ok(this.orderITemService.ConfirmPurchaseItem());
    }

    /**
     * 取消购买
     * @return true or false
     */
    @GetMapping("/removeCache")
    public ResponseEntity<String> removeCache() {
        return ResponseEntity.ok(this.orderITemService.removeCache());
    }

    /**
     * Echarts饼状图数据接口
     * @return
     */
    @GetMapping("/pie")
    public ResponseEntity<List<DashboardVo>> pie(){
        return ResponseEntity.ok(this.orderItemService.CountByCategoryId());
    }
}

